import {
	mapState,
	mapMutations
} from "vuex";

export default {
	data() {
		return {

			pageHeight: null,
			tabbarHeight: null,
			msgType: 'success',
			messageText: '',
			IsBottom: false,
			// params: null,
		};
	},
	computed: {
		...mapState(["isLogin", "backTab"]),
	},
	onLoad(params) {
		let route = getCurrentPages()[getCurrentPages().length - 1].route;
		let isTabbar = [
			"pages/home/Home",
			"pages/category/Category",
			"pages/cart/Cart",
			"pages/my/My",
		].includes(route);
		if (isTabbar) {
			if (Array.isArray(uni.getStorageSync('backTab'))) {
				let tabbars = uni.getStorageSync('backTab');
				let idx = tabbars.indexOf(route);
				console.log('tabbars',idx)
				if (idx != -1)
					tabbars.slice(idx, 1);
				else
					tabbars.push(route);
				uni.setStorageSync('backTab', tabbars);
			} else {
				let tabbars = [route];
				uni.setStorageSync('backTab', tabbars);
			}
		}

		this.pageHeight = uni.getStorageSync('pageHeight');
	},
	methods: {
		back() {
			let route = getCurrentPages()[getCurrentPages().length - 1].route;
			let isTabbar = [
				"pages/home/Home",
				"pages/category/Category",
				"pages/cart/Cart",
				"pages/my/My",
			].includes(route);
			if (isTabbar) {
				uni.switchTab({
					url: `/${route}`
				})
			} else if (route.includes('Detail') &&
				uni.getStorageSync('goodIds').length
			) {
				let goodIds = uni.getStorageSync('goodIds');
				let id = goodIds.splice(-1, 1)[0];
				uni.setStorageSync('goodIds', goodIds);
				uni.redirectTo({
					url: `/pages/detail/Detail?id=${id}`
				})
			} else if (getCurrentPages().length == 1) {
				uni.switchTab({
					url: "/pages/home/Home"
				})
			} else {
				uni.navigateBack({
					delta: 1,
				});
			}
			// if (uni.getStorageSync('backTab')) {
			// 	let tabbars = uni.getStorageSync('backTab');
			// 	if (tabbars.length == 1) {
			// 		uni.switchTab({
			// 			url: `/${tabbars[0]}`
			// 		})
			// 	} else {
			// 		let tabbar = tabbars.shift();
			// 		uni.switchTab({
			// 			url: `/${tabbar}`
			// 		})
			// 	}
			// }

		},
		toMy() {
			if (uni.getStorageSync('token') && this.isLogin) {
				uni.switchTab({
					url: "/pages/my/My",
				})
			} else {
				uni.navigateTo({
					url: "/pages/user/Login",
				})
			}
		},
		toCollection() {
			if (uni.getStorageSync('token') && this.isLogin) {
				uni.switchTab({
					url: "/pages/my/childCpn/MyCollect",

				})
			} else {
				uni.navigateTo({
					url: "/pages/user/Login",
				})
			}
		},
		toViewed() {
			if (uni.getStorageSync('token') && this.isLogin) {
				uni.switchTab({
					url: "/pages/my/childCpn/MyViewed",
				})
			} else {
				uni.navigateTo({
					url: "/pages/user/Login",
				})
			}
		},
		reload() {
			uni.navigateBack({
				delta: 0
			})
			// this.$router.go(0);
		},
		backTop() {
			uni.pageScrollTo({
				duration: 100,
				scrollTop: 0
			});
		},
		success(title, duration = 1000) {
			return new Promise((resolve, reject) => {
				uni.showToast({
					icon: "success",
					title,
					duration
				})
				setTimeout(() => {
					resolve();
				}, duration)
			})

		},
		error(title, duration = 1000) {
			return new Promise((resolve, reject) => {
				uni.showToast({
					icon: "error",
					title,
					duration
				})
				setTimeout(() => {
					resolve();
				}, duration)
			})

		},
		info(title, duration = 1000) {
			return new Promise((resolve, reject) => {
				uni.showToast({
					icon: "none",
					title,
					duration
				})
				setTimeout(() => {
					resolve();
				}, duration)
			})
		},

	}
};
